<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="登录"
    >
      <van-icon
        slot="left"
        name="cross"
        @click="$router.back()"
      />
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 登录表单 -->
    <van-form
      @submit="onSubmit"
      ref="loginForm"
    >
      <van-field
        v-model="user.mobile"
        name="mobile"
        type="number"
        maxlength="11"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
      >
        <!-- 通过插槽处理自定义图标 -->
        <i
          slot="left-icon"
          class="toutiao toutiao-shouji"
        ></i>
      </van-field>
      <!-- maxlength是H5属性 规定文本最大输入长度 -->
      <van-field
        v-model="user.code"
        type="number"
        maxlength="6"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
      >
        <i
          slot="left-icon"
          class="toutiao toutiao-yanzhengma"
        ></i><!-- 通过插槽处理自定义图标 -->
        <template #button>
          <!-- time 倒计时时间 ss代表秒 s代表单位 -->
          <van-count-down
            v-if="isCountDownShow"
            :time="1000 * 60"
            format="ss s"
            @finish="isCountDownShow = false"
          ></van-count-down>
          <!-- 新版本插槽的写法 -->
          <van-button
            v-else
            class="send-sms-btn"
            round
            size="small"
            type="default"
            @click="onSendSms"
            native-type="button"
          >发送验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button
          class="login-btn"
          block
          type="info"
          native-type="submit"
        >
          登录
        </van-button>
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'
export default {
  name: 'LoginIndex',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '', // 手机号
        code: ''// 验证码
      },
      userFormRules: {
        mobile: [{ required: true, message: '手机号不能为空' }, {
          pattern: /^1[3578]\d{9}$/,
          message: '手机号格式错误'
        }
        ],
        code: [{ required: true, message: '验证码不能为空' }, {
          pattern: /^\d{6}$/,
          message: '验证码格式错误'
        }]
      },
      isCountDownShow: false // 是否展示倒计时
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    async onSubmit (values) {
      // console.log('submit', values)
      this.$toast.loading({
        duration: 0, // 提示框的持续时间 0表示持续展示不停止 指导成功或失败 才可以关闭
        forbidclick: true, // 表示 是否禁止点击背景
        message: '登陆中...'// 提示信息
      })
      try {
        const { data } = await login(this.user)
        // console.log('登录成功', data.data)
        // 调用setUser函数 更改state中的user值
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功！')
        this.$router.back()
      } catch (err) {
        if (err.response.status === 400) {
          // console.log('手机号或验证码错误', err)
          this.$toast.fail('手机号或验证码错误')
        } else {
          // console.log('登陆失败，请稍后重试', err)
          this.$toast.fail('登录失败，请稍后重试！')
        }
      }
    },
    async onSendSms () {
      // 1.校验手机号
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.log('手机号格式有误', err)
      }
      // 手机号验证通过，显示倒计时
      this.isCountDownShow = true
      //
      try {
        await sendSms(this.user.mobile)
        this.$toast.success('发送成功！')
      } catch (err) {
        // 发送失败，关闭倒计时按钮
        this.isCountDownShow = false
        if (err.response.status === 429) {
          // $toast.fail 属于Vant中的错误提示 带有图标
          this.$toast.fail('发送太过频繁，请稍后重试！')
        } else {
          // $toast属于 Vant中的轻提示
          this.$toast('发送失败，请稍后重试！')
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  /*   .page-nav-bar {
    background-color: #3296fa;
  } */
  .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }

  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
